package com.well.rpc.utils;

import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.springframework.util.Assert;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

public class ThreadPoolUtils {

    private static Integer DEFAULT_THREAD_POOL_SIZE =  Runtime.getRuntime().availableProcessors() * 2;

    private static ScheduledExecutorService threadPool = new ScheduledThreadPoolExecutor(DEFAULT_THREAD_POOL_SIZE, new BasicThreadFactory.Builder()
            .namingPattern("Client-ThreadFactory-%d").build());

    public static ScheduledExecutorService scheduledThreadPool() {
        return threadPool;
    }

    /**
     * @param runnable runnable
     */
    public static void execute(Runnable runnable) {
        Assert.notNull(runnable, "runnable is not null!");
        scheduledThreadPool().execute(runnable);
    }

}
